package com.app.micaihu.view.newsdetail.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;


import androidx.annotation.Nullable;

import com.android.volley.VolleyError;
import com.app.config.LoadViewConfig;
import com.app.micaihu.R;
import com.app.micaihu.adapter.VideoAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;

import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.view.newsdetail.VideoDetailActivity;
import com.app.micaihu.view.user.userinfo.fragment.HeaderViewBaseFragment;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guo on 2017/1/4.
 * 讲堂的fragment
 */

public class VideoShowFragment2 extends HeaderViewBaseFragment<NewsEntity> implements AdapterView.OnItemClickListener {
    protected static final String ARG_ID = "id";
    protected static final String ARG_NUM = "setNum";

    //数据
    private String id;
    private String setNum;
    private VideoAdapter mAdapter;

    public VideoShowFragment2() {
    }


    public static VideoShowFragment2 newInstance( String id, String setNum) {

        VideoShowFragment2 fragment = new VideoShowFragment2();
        Bundle args = new Bundle();

        args.putString(ARG_ID, id);
        args.putString(ARG_NUM, setNum);


        fragment.setArguments(args);
        return fragment;
    }


    public void initData(List<NewsEntity> list) {
        if (list != null) {
            if (mDatalist == null) {
                mDatalist = new ArrayList<>();
            }
            mDatalist.addAll(list);
        }
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        id = getArguments().getString(ARG_ID);
        setNum = getArguments().getString(ARG_NUM);
        if(mListView == null || loadMoreView == null || lvLoading == null) return;
        mListView.setOnItemClickListener(this);
        if (mDatalist == null || mDatalist.size() <= 0) {
            loadData(true);
        } else {

            if (mDatalist.size() > 0 && mListView != null) {
                if (mAdapter == null) {
                    mAdapter = new VideoAdapter(mDatalist, getActivity());
                    try {
                        mListView.setAdapter(mAdapter);
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                } else {
                    mListView.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                }
                onComplete(1, null);
            }

        }
    }




    @Override
    public void loadData(final boolean refresh) {
        Map<String, String> parms = new HashMap<>();
        parms.put("page", page + "");
        parms.put("id", id);
        parms.put("setNum", setNum);
        HttpListener<DataBean<List<NewsEntity>>> httpListener = new HttpListener<DataBean<List<NewsEntity>>>() {
            @Override
            public void onStart() {
                //设置正在刷新
                onRefreshing(null);
            }

            @Override
            public void onError(VolleyError error) {
                //设置刷新状态
                if(mDatalist == null || mDatalist.isEmpty()){
                    mListView.setEmptyView(lvLoading);
                    lvLoading.showErrorPage();
                    isRefreshing = false;
                }else{
                    onComplete(2, null);

                }
            }

            @Override
            public void onSuccess(DataBean<List<NewsEntity>> response) {

                if (response.noError() && response.getData() != null) {
                    List<NewsEntity> videolist = response.getData();
                    //数据去重
                    dataRevisited(videolist);

                    if (videolist.size() > 0) {
                        if (mDatalist == null) {
                            mDatalist = new ArrayList<>();
                        }
                        if (refresh) {
                            mDatalist.clear();
                        }
                        mDatalist.addAll(videolist);
                        if (mAdapter == null) {
                            mAdapter = new VideoAdapter(mDatalist, getActivity());
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }

                        onComplete(1, "");//展示底部空view
                    } else {
                        if (mDatalist == null || mDatalist.isEmpty()) {//展示空view
                            mListView.setEmptyView(lvLoading);
                            lvLoading.showEmptyPage(LoadViewConfig.EMPTY_DEFAULT, AppApplication.getInstance().getString(R.string.vidoe_empty));
                            isRefreshing = false;

                        } else {

                            onComplete(2, AppApplication.getInstance().getString(R.string.pullup_nomore_data));//展示底部空view
                        }
                    }
                } else {
                    if (mDatalist == null || mDatalist.isEmpty()) {//展示errorPage
                        mListView.setEmptyView(lvLoading);
                        lvLoading.showErrorPage();
                        isRefreshing = false;
                    } else {
                        onComplete(2,AppApplication.getInstance().getString(R.string.pullup_nomore_data));
                    }
                }
            }
        };
        HttpUtils.addPostCacheRequest(URIContant.VIDEO_SHOW_LIST, new TypeToken<DataBean<List<NewsEntity>>>() {
        }.getType(), "", parms, httpListener);

    }

    /**
     * 数据去重
     *
     * @param list
     */
    private void dataRevisited(List<NewsEntity> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size()) {
                break;
            }
            NewsEntity ne = list.get(i);
            if (ne == null) {
                continue;
            }
            if (!TypeConstant.getVideoIsPlay(ne)) {
                //如果视频不是规定的类型则移除
                list.remove(i);
                i--;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if(mDatalist == null || mListView == null ){
            return;
        }
        //因为有头部
        if (mListView.getHeaderViewsCount() > 0) {
            position = position - mListView.getHeaderViewsCount();
        }
        if(position < 0){
            position = 0;
        }else if(position >= mDatalist.size()){
            position = mDatalist.size()-1;
        }
        NewsEntity newsEntity = mDatalist.get(position);
        if(newsEntity == null || getActivity() == null){
            return;
        }
        Intent intent = new Intent(getActivity(), VideoDetailActivity.class);
        intent.putExtra(Config.Extra.PARAMETER1,newsEntity.getArticleId());
        intent.putExtra(Config.Extra.PARAMETER2,TypeConstant.SOURCETYPE_NORMAL);
        startActivity(intent);
    }
}
